//
//  HContactDetailViewController.m
//  NursingManages
//
//  Created by HuangZhen on 2017/5/26.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HContactDetailViewController.h"

@interface HContactDetailViewController ()

@property NSInteger kContactHeadviewHeight;

@end

@implementation HContactDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.myTableView setTableHeaderView: self.GetiPhoneSytleHeadView];
    [self.view addSubview:self.myTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


-(instancetype)initWithModel:(Model_Pub_Employee *)employee{

    if (self) {
        self.theEmployee = employee;
        
        self.kContactHeadviewHeight = 200;
    }
    return self;
}


- (UITableView *)myTableView
{
    if (!_myTableView ) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - 50)];
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        _myTableView.tableFooterView = [[UIView alloc]init];
    }
    return _myTableView;
}

#pragma mark UITableView 头部姓名和拨号，仿iPhone联系人明细界面

-(UIImageView *) GetHeadImageView{

    NSInteger picHeight = 80;
    NSInteger picWidth = picHeight;

    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, picWidth, picHeight)];
    
    imageV.layer.cornerRadius = picWidth / 2;
    imageV.layer.masksToBounds = YES;
    
    [imageV setImage:[UIImage imageNamed:@"icon"]];
    return imageV;
}

-(UIView *) GetPhoneMessageView{
    
    NSInteger width = ScreenWidth;
    NSInteger picHeight = 30;
    NSInteger picWidth = 30;
    NSInteger picLeft = 50;
    UIView *view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, 40)];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(picLeft, 0, picWidth, picHeight);
    [button1 setImage:[UIImage imageNamed:@"iphone"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(callme:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(picLeft + picWidth + picLeft, 0, picWidth, picHeight);
    [button2 setImage:[UIImage imageNamed:@"sms"] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(sms:) forControlEvents:UIControlEventTouchUpInside];
    if (self.theEmployee.MOBILE_PHONE.length > 0) {
        [view addSubview:button1];
        [view addSubview:button2];
        view.userInteractionEnabled = YES;

    }
    return view;
}
- (void)sms:(UIButton *)sender{
    
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"sms:%@",self.theEmployee.MOBILE_PHONE];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}
- (void)callme :(UIButton *)seder
{
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.theEmployee.MOBILE_PHONE];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}

-(UIView *) GetiPhoneSytleHeadView{
   
    NSUInteger tempTop = NavBarHeight;
    
    UIView *headview = [[UIView alloc]initWithFrame:CGRectMake(0, tempTop,300, _kContactHeadviewHeight)];
    
    
    UIImageView *headPic = [self GetHeadImageView];
    headPic.center = self.view.center;
    headPic.top = 20;
    
    tempTop = headview.frame.origin.y + 40;
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
    nameLabel.text = self.theEmployee.EMP_NAME;
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.center = self.view.center;
    nameLabel.top = tempTop;
    
    tempTop = nameLabel.frame.origin.y + 50;
    
    UIView *phoneSmsView = [self GetPhoneMessageView];
    phoneSmsView.center = self.view.center;
    phoneSmsView.top = tempTop;
    
    [headview addSubview:headPic];
    [headview addSubview:nameLabel];
    [headview addSubview:phoneSmsView];
    return headview;
}


#pragma mark UITableView 委托


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"工号";
            cell.detailTextLabel.text = self.theEmployee.JUB_NUM;

            break;
            
        case 1:
            cell.textLabel.text = @"科室";
            cell.detailTextLabel.text = self.theEmployee.DEPT_NAME;
            
            break;
            
        case 2:
            
            cell.textLabel.text = @"电话";
            cell.detailTextLabel.text = self.theEmployee.MOBILE_PHONE;
            
            break;
            
        case 3:

            cell.textLabel.text = @"住址";
            cell.detailTextLabel.text = self.theEmployee.ADDRESS;
            
            break;

        default:
            break;
    }
    
    
    return cell;
}

@end
